package cn.ac.cintcm.dataconverge.dataAnalyze.domain;

import java.util.Iterator;
import java.util.List;

import com.hs.core.model.BaseBean;

public class TagTree extends BaseBean{
	private TagNode root;
	private String treeName;
	
	public TagTree(String treeName){
		root=new TagNode(0l,"",null);
		this.treeName=treeName;
	}	
	
	public TagTree(TagNode root,String treeName){
		this.root=root;
		this.treeName=treeName;
	}
	
	public TagNode getRoot() {
		return root;
	}
	public void setRoot(TagNode root) {
		this.root = root;
	}
	public String getTreeName() {
		return treeName;
	}
	public void setTreeName(String treeName) {
		this.treeName = treeName;
	}
	
	public TagNode appendTagNode(Long tagId,String tagName,TagNode parent,int subjectNum,int orgId){
		if(parent==null){
			parent=root;
		}
		TagNode node=new TagNode(tagId,tagName,parent);
		node.setParent(parent);
		node.setSubjectNum(subjectNum);
				
		parent.getChildNodes().add(node);
//		parent.setSubjectNum(parent.getSubjectNum()+subjectNum);
//		
//		while(parent.getParent()!=null){
//			parent=parent.getParent();
//			parent.setSubjectNum(parent.getSubjectNum()+subjectNum);
//		}
		return node;
	}
	
	public TagNode appendTagNode(Long tagId,String tagName,TagNode parent,int subjectNum,float permitFund){
		if(parent==null){
			parent=root;
		}
		TagNode node=new TagNode(tagId,tagName,parent);
		node.setParent(parent);
		node.setSubjectNum(subjectNum);
		node.setPermitFund(permitFund);
		
		parent.getChildNodes().add(node);
//		parent.setSubjectNum(parent.getSubjectNum()+subjectNum);
//		parent.setPermitFund(parent.getPermitFund()+permitFund);
//		
//		while(parent.getParent()!=null){
//			parent=parent.getParent();
//			parent.setSubjectNum(parent.getSubjectNum()+subjectNum);
//			parent.setPermitFund(parent.getPermitFund()+permitFund);
//		}
		
		return node;		
	}
	
	public TagNode appendTagNode(Long tagId,String tagName,TagNode parent){
		if(parent==null){
			parent=root;
		}
		TagNode node=new TagNode(tagId,tagName,parent);
		node.setParent(parent);
				
		parent.getChildNodes().add(node);		
		return node;
	}
	
/*	public void clearSubNum(){
		if(this==null)
			return;
		TagNode root=this.getRoot();
		clearSubNum(root);
	}
	private void clearSubNum(TagNode node){
		node.setSubjectNum(0);
		LinkedList childNodes=node.getChildNodes();
		if(childNodes!=null && childNodes.size()>0){
			Iterator iter=childNodes.iterator();
			while(iter.hasNext()){
				TagNode node1=(TagNode)iter.next();
				clearSubNum(node1);
			}
		}
		
	}*/
	
	public TagNode getNodeByTagId(int tagId){
		TagNode root=this.getRoot();
		return getNode(root,tagId);		
	}
	
	private TagNode getNode(TagNode node,int tagId){
		if(node==null){
			return null;
		}
		if(node.getTagId()==tagId){
			return node;
		}
		
			List childNodes=node.getChildNodes();
			if(childNodes==null ||childNodes.size()==0){
				return null;
			}
			Iterator iter=childNodes.iterator();
			while(iter.hasNext()){
				TagNode childNode=(TagNode)iter.next();
				if(getNode(childNode,tagId)!=null)
				return getNode(childNode,tagId);
			}
	
		return null;
	}
}
